﻿using System;
using System.Globalization;
using System.Windows.Forms;
using System.Diagnostics;

namespace EastFantasy.EasyEngine.UI
{
    public partial class ChooseProcess : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        /// 选择的进程
        /// <summary>
        /// 选择的进程
        /// </summary>
        public Process Process;

        /// 进程列表
        /// <summary>
        /// 进程列表
        /// </summary>
        private Process[] _processes;

        public ChooseProcess()
        {
            InitializeComponent();
        }

        #region 窗体载入获取进程列表

        /// <summary>
        /// 窗体载入获取进程列表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ChooseProcess_Load(object sender, EventArgs e)
        {
            _processes = cbAll.Checked ? Process.GetProcesses() : Process.GetProcessesByName("dnf");
            lvProcess.BeginUpdate();
            foreach (Process t in _processes)
            {
                ListViewItem item = new ListViewItem(t.ProcessName);
                try
                {
                    item.SubItems.Add(t.StartTime.ToString("MM-dd HH:mm:ss"));
                    item.SubItems.Add(t.Id.ToString(CultureInfo.InvariantCulture));
                }
                catch (Exception)
                {
                    item.SubItems.Add("未知");
                    item.SubItems.Add(t.Id.ToString(CultureInfo.InvariantCulture));
                }
                lvProcess.Items.Add(item);
            }
            lvProcess.EndUpdate();
        }

        #endregion

        #region 显示所有进程

        /// <summary>
        /// 显示所有进程
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cbAll_CheckedChanged(object sender, EventArgs e)
        {
            lvProcess.Items.Clear();
            Process[] ps = cbAll.Checked ? Process.GetProcesses() : Process.GetProcessesByName("dnf");
            lvProcess.BeginUpdate();
            foreach (Process t in ps)
            {
                ListViewItem item = new ListViewItem(t.ProcessName);
                try
                {
                    item.SubItems.Add(t.StartTime.ToString("MM-dd HH:mm:ss"));
                    item.SubItems.Add(t.Id.ToString(CultureInfo.InvariantCulture));
                }
                catch (Exception)
                {
                    item.SubItems.Add("未知");
                    item.SubItems.Add(t.Id.ToString(CultureInfo.InvariantCulture));
                }
                lvProcess.Items.Add(item);
            }
            lvProcess.EndUpdate();
        }

        #endregion

        #region 选择进程(ListView)

        /// <summary>
        /// 选择进程(ListView)
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void lvProcess_DoubleClick(object sender, EventArgs e)
        {
            Process = Process.GetProcessById(Convert.ToInt32(lvProcess.SelectedItems[0].SubItems[2].Text));
            DialogResult = DialogResult.OK;
            Close();
        }

        #endregion

        #region 选择进程(按钮）

        /// <summary>
        /// 选择进程(按钮）
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSelect_Click(object sender, EventArgs e)
        {
            if (lvProcess.SelectedItems.Count <= 0)
            {
                Message.Show(this, "错误", "请选择进程！");
                return;
            }
            Process = Process.GetProcessById(Convert.ToInt32(lvProcess.SelectedItems[0].SubItems[2].Text));
            DialogResult = DialogResult.OK;
            Close();
        }

        #endregion
    }
}